﻿<%@ WebHandler Language="C#" Class="ZSFullSetStatisticsAnalysisHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
/// <summary>
/// add by longb 2022-06-07 
/// 齐套统计分析
/// </summary>
public class ZSFullSetStatisticsAnalysisHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    FullStatisticsAnalysisBusiness m_fullab = new FullStatisticsAnalysisBusiness();
    CollectERPConfirmBusines r_Mconfirm = new CollectERPConfirmBusines();
    #region 页面初始化
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                
                case "GetTaskOrderData": //任务单信息
                    GetTaskOrderData(context);
                    break;
                case "GetBigSmallData": //查询物大小件信息
                    GetBigSmallData(context);
                    break;
                case "GetSpec"://查询工序信息
                    GetSpec(context);
                    break;
                case "SetFullTime"://设置齐套率时间
                    SetFullTime(context);
                    break;
                case "GetIssueMaterialLackQueryData":
                    GetIssueMaterialLackQueryData(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    #endregion

    #region 查询方法
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    protected void GetTaskOrderData(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", dt.Rows[0]["ContainerNo"].ToString());
        Condition.Add("Machine", dt.Rows[0]["Machine"].ToString());
        Condition.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        Condition.Add("usecompleted", dt.Rows[0]["usecompleted"].ToString());
        Condition.Add("PageSize", context.Request["pageSize"].ToString());
        Condition.Add("CurrentPageIndex", context.Request["pageNumber"].ToString());
        uMESProcessResult result = m_fullab.GetTaskOrderDataByPagesize(Condition);
        //for (var i = 0; i < result.DBTable.Rows.Count; i++)
        //{
        //    var para = new Dictionary<string, string>();
        //    para.Add("ContainerNo", result.DBTable.Rows[i]["containerNo"].ToString());//任务单号
        //    para.Add("ContainerID", result.DBTable.Rows[i]["containerid"].ToString());
        //    para.Add("workflowid", result.DBTable.Rows[i]["workflowid"].ToString());
        //    DataTable dtt= m_fullab.GetSpec(para);
        //    DataView spec = dtt.DefaultView;
        //    spec.RowFilter = "(PCOUNT<>0 AND (PCOUNT=CCOUNT))";//查询工序下有大小件信息（报工==需求）条数
        //    DataTable dtl = spec.ToTable();

        //    DataView Aspec = dtt.DefaultView;
        //    Aspec.RowFilter = "PCOUNT<>0";
        //    DataTable dtll = Aspec.ToTable();
            
        //    Double QTL =dtll.Rows.Count == 0 ? 0 : ((1.0000*dtl.Rows.Count / dtll.Rows.Count) * 100);
        //    result.DBTable.Rows[i]["QTL"] = QTL;
        //    DataTable bigsmall = m_fullab.GetBigSmallData(para);
        //    var maxtime = bigsmall.Compute("Max(fulltime)", "true").ToString();
        //    result.DBTable.Rows[i]["FULLTIME"] = maxtime;
        //}
        if (result.Result) result.Message = "查询成功";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    protected void GetBigSmallData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        string ContainerID = context.Request["ContainerID"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ContainerNo", ContainerNo);//任务单号
        para.Add("ContainerID", ContainerID);//任务单号
        para.Add("specid", workflowstepid);
        para.Add("mfgorderid", context.Request["mfgorderid"].ToString());
        DataTable dt = m_fullab.GetBigSmallData(para);
        DataView big = dt.DefaultView;
        big.RowFilter = "PARTTYPE='0'";
        result.DBTable = big.ToTable();
        DataView small = dt.DefaultView;
        small.RowFilter = "PARTTYPE='1'";
        result.ReturnData = small.ToTable();
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询工序信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetSpec(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string workflowid = context.Request["workflowid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        string ContainerId = context.Request["ContainerId"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ContainerNo", ContainerNo);//任务单号
        para.Add("ContainerID", ContainerId);
        para.Add("workflowid", workflowid);
        para.Add("specid", "");
        para.Add("mfgorderid", context.Request["mfgorderid"].ToString());
        DataTable bs = m_fullab.GetBigSmallData(para);
        var Cmaxtime = bs.Compute("Max(fulltime)", "true").ToString();
        result.Message = Cmaxtime;
        result.DBTable = m_fullab.GetSpec(para);
        for (var i = 0; i < result.DBTable.Rows.Count; i++)
        {
            para["specid"] = result.DBTable.Rows[i]["SPECID"].ToString();
            DataTable bigsmall = m_fullab.GetBigSmallData(para);
            var maxtime = bigsmall.Compute("Max(fulltime)", "true").ToString();
            result.DBTable.Rows[i]["FULLTIME"] = maxtime;
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 设置预齐套时间
    /// </summary>
    /// <param name="context"></param>
    protected void SetFullTime(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string partmenuinfoid = context.Request["partmenuinfoid"].ToString();
        string time = context.Request["time"].ToString();
        var Condition = new Dictionary<string, string>();
        Condition.Add("time", time);//
        Condition.Add("partmenuinfoid", partmenuinfoid);
        result.Result=m_fullab.SetBigSmallFullTime(Condition);
        result.Message = result.Result ? "设置成功" : "设置失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 缺件统计（任务单）分页
    /// </summary>
    /// <param name="context"></param>
    protected void GetIssueMaterialLackQueryData(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", dt.Rows[0]["ContainerNo"].ToString());
        Condition.Add("Machine", dt.Rows[0]["Machine"].ToString());
        Condition.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        Condition.Add("WORKFLOWTYPE", dt.Rows[0]["WORKFLOWTYPE"].ToString()=="0"?"临时":"正常");
        Condition.Add("PageSize", pageSize);
        Condition.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result = new uMESProcessResult();
        result = m_fullab.GetTaskOrderDataByPagesize(Condition);
        //for (var i = 0; i < result.DBTable.Rows.Count; i++)
        //{
        //    var para = new Dictionary<string, string>();
        //    para.Add("ContainerNo", result.DBTable.Rows[i]["containerNo"].ToString());//任务单号
        //    para.Add("ContainerID", result.DBTable.Rows[i]["containerid"].ToString());
        //    para.Add("workflowid", result.DBTable.Rows[i]["workflowid"].ToString());
        //    DataTable dtt = m_fullab.GetSpec(para);
        //    DataView spec = dtt.DefaultView;
        //    spec.RowFilter = "PCOUNT<>0 AND (PCOUNT=CCOUNT)";
        //    DataTable dtl = spec.ToTable();
        //    Double QTL = dtt.Rows.Count == 0 ? 0 : ((1.0000 * dtl.Rows.Count / dtt.Rows.Count) * 100);
        //    result.DBTable.Rows[i]["QTL"] = QTL;
        //}
        if (result.Result) result.Message = "查询成功";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    #endregion
}